package cn.wolfcode.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.wolfcode.business.domain.Appointment;
import cn.wolfcode.business.mapper.AppointmentMapper;
import cn.wolfcode.business.mapper.StatementItemMapper;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import cn.wolfcode.common.exception.ServiceException;
import cn.wolfcode.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.StatementMapper;
import cn.wolfcode.business.domain.Statement;
import cn.wolfcode.business.service.IStatementService;

import javax.swing.plaf.nimbus.State;

/**
 * 结算单Service业务层处理
 * 
 * @author dafei
 * @date 2024-03-04
 */
@Service
public class StatementServiceImpl implements IStatementService 
{
    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 查询结算单
     * 
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public Statement selectStatementById(Long id)
    {
        return statementMapper.selectStatementById(id);
    }

    /**
     * 查询结算单列表
     * 
     * @param statement 结算单
     * @return 结算单
     */
    @Override
    public List<Statement> selectStatementList(Statement statement)
    {
        return statementMapper.selectStatementList(statement);
    }

    /**
     * 新增结算单
     * 
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int insertStatement(Statement statement)
    {
        // 手机格式合法性校验
        if (!RegexUtils.isPhoneLegal(statement.getCustomerPhone())){
            throw new ServiceException("手机号码格式不正确！");
        }
        // 车牌号格式校验
        if (VehiclePlateNoUtil.getVehiclePlateNo(statement.getLicensePlate()) == null){
            throw new ServiceException("车牌号码不合法！");
        }
        // 到店时间  小于 当前时简
        if( statement.getActualArrivalTime().getTime() - new Date().getTime() > 0){
            throw new ServiceException("到店时间要小于当前时间！");
        }



        //设置默认值
        statement.setStatus(Statement.STATUS_CONSUME);
        statement.setTotalAmount(BigDecimal.ZERO);
        statement.setTotalQuantity(BigDecimal.ZERO);
        statement.setDiscountAmount(BigDecimal.ZERO);

        statement.setCreateTime(DateUtils.getNowDate());
        return statementMapper.insertStatement(statement);
    }

    /**
     * 修改结算单
     * 
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int updateStatement(Statement statement)
    {


        Statement st = statementMapper.selectStatementById(statement.getId());
        if(st == null){
            throw new ServiceException("参数异常");
        }

        if(st.getStatus() != Statement.STATUS_CONSUME){
            throw new ServiceException("必须是消费中结算单才允许修改");
        }

        // 手机格式合法性校验
        if (!RegexUtils.isPhoneLegal(statement.getCustomerPhone())){
            throw new ServiceException("手机号码格式不正确！");
        }
        // 车牌号格式校验
        if (VehiclePlateNoUtil.getVehiclePlateNo(statement.getLicensePlate()) == null){
            throw new ServiceException("车牌号码不合法！");
        }
        // 到店时间  小于 当前时简
        if( statement.getActualArrivalTime().getTime() - new Date().getTime() > 0){
            throw new ServiceException("到店时间要小于当前时间！");
        }

        return statementMapper.updateStatement(statement);
    }

    /**
     * 批量删除结算单
     * 
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementByIds(Long[] ids)
    {
        return statementMapper.deleteStatementByIds(ids);
    }

    /**
     * 删除结算单信息
     * 
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementById(Long id)
    {
        return statementMapper.deleteStatementById(id);
    }

    @Override
    public void delete(Long id) {

        //1：结算单删除
        Statement statement = statementMapper.selectStatementById(id);
        if(statement == null){
            throw new ServiceException("参数异常");
        }
        statementMapper.deleteStatementById(id);


        //2: 结算单明细删除
        statementItemMapper.deleteByStatementId(id);

        //3：如果绑定预约单--改为：已到店 状态
        if(statement.getAppointmentId() != null){
            Appointment appointment = appointmentMapper.selectAppointmentById(statement.getAppointmentId());
            appointment.setStatus(Appointment.STATUS_ARRIVAL);
            appointmentMapper.updateAppointment(appointment);
        }
    }
}
